const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    daylist: [100, 90, 80, 70, 60, 50, 40, 30, 20, 10],
    endtime: "2021/11/29,08:00:00"
  },
  getPhoneNumber(e) {
    var that = this;
    var fenxiao = that.data.area;
    var geneal = that.data.geneal;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      wx.setStorageSync('dayphone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=41045&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: fenxiao,
          geneal: geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          that.setData({
            fixed: !1
          })
        }
      })

      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 8,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
    }).catch(err => {
      console.error(err);
    });
  },

  ontap: function (e) {
    var that = this;
    var phone = wx.getStorageSync('dayphone')
    if (phone) {
      var day = e.currentTarget.dataset.index
      wx.showModal({
        title: '提示',
        content: '是否预约倒计时提醒',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.requestSubscribeMessage({
              tmplIds: ['vkxr9XBBA4OgDQqtDq2R8Q-FPpfU40da5w31CqDTMMQ'], //报名提醒
              success(res) {
                console.log(res);
                db.collection('gkdjs').where({
                  _openid: app.globalData.openid,
                }).get({
                  success: res => {
                    var that = this;
                    db.collection('gkdjs').add({
                      data: {
                        day: day,
                      },
                      //添加成功的回调函数
                      success(res) {
                        console.log(e.currentTarget)
                        wx.showToast({
                          title: '预约成功！',
                        })
                      }
                    });
                  },
                })
              },
              fail(res) {
                console.log(res);
              }
            })
            wx.request({ //发送请求
              url: 'https://zg99.offcn.com/index/biaodan/writelogs?actid=41045&callback=?', //接收url
              data: {
                phone: phone,
                day: day
              }, //接收参数
              success(res) {
                var jsonStr = res.data;
                jsonStr = jsonStr.replace("(", "");
                jsonStr = jsonStr.replace(")", "");
                if (typeof jsonStr != 'object') {
                  jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                  var theJson = JSON.parse(jsonStr);
                }
                console.log(theJson)

              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showToast({
        title: '请先登录！',
        icon: 'none'
      })
      that.setData({
        fixed: 1
      })
    }

  },

  countdown: function () {
    var _ = this;
    var endtime = new Date(_.data.endtime); //结束时间
    var nowtime = new Date(); //当前时间
    var lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000);

    var d = parseInt(lefttime / 3600 / 24);

    if (d < 100) {
      var s = d.toString();
      d = '0' + s
    }
    console.log(d)
    var s = d.toString();
    var a = s.substring(0, 1); //str
    var b = s.substring(1, 2); //str
    var c = s.substring(2, 3); //str
    _.setData({
      a,
      b,
      c
    })


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.countdown()
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent,
      day100: wx.getStorageSync('day100'),
      day90: wx.getStorageSync('day90'),
      day80: wx.getStorageSync('day80'),
      day70: wx.getStorageSync('day70'),
      day60: wx.getStorageSync('day60'),
      day50: wx.getStorageSync('day50'),
      day40: wx.getStorageSync('day40'),
      day30: wx.getStorageSync('day30'),
      day20: wx.getStorageSync('day20'),
      day10: wx.getStorageSync('day10'),
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})